package com.Pratice;
import java.util.Scanner;

public class PREV55 {
	static int N = 10;
	static String math = "ADD";
	private static char[] array = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
	private static String strNum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	public static void main(String[] args) {
		long res = 0;
		Scanner scan = new Scanner(System.in);
		int n= scan.nextInt();
		while(n-- > 0)
		{
			String cmd = scan.next();
			switch(cmd){
				case "CHANGE":N = scan.nextInt(); break;
				case "EQUAL":System.out.println(transformTo_N(res, N));break;
				case "CLEAR":res = 0;break;
				case "NUM":res = getMath(scan.next(),res,math);break;
				default:math = cmd;break;
			}
		}
	}
	public static long getMath(String num,long res,String math) {
			switch(math){
				case "ADD":res = res + N_to_10(num,N);break;
				case "SUB":res = res - N_to_10(num,N);break;
				case "MUL":res = res * N_to_10(num,N);break;
				case "MOD":res = res % N_to_10(num,N);break;
				case "DIV":res = res / N_to_10(num,N);break;
			}
			return res;
	}
	public static long N_to_10(String num,int N) {
		long res = 0;
		char[] ch = num.toCharArray();
		long base = 1;
		for (int i = ch.length - 1; i >= 0; i--) {
			long index = strNum.indexOf(ch[i]);
			res+= index*base;
			base = N*base;
		}
		return res;
	}
	public static String transformTo_N(long num,int N) {
		String res = "";
		for (long i = num; i> 0; i/=N) {
			res = array[(int) (i%N)] +res;
		}
		return res;
	}
	
}
